using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using eShop.Business;
using eShop.Model;
using eShop.Business;
using Entity;
using eShop.Utility;
using System.Web;

public partial class receipt : SitePublicBasePage
{
    private Int32 OrderNo
    {
        get
        {
            try
            {
                if (SessionManager.OrderID != null)
                    return SessionManager.OrderID;
                else if (HttpContext.Current.Request.QueryString["oid"] != null && !String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["oid"]))
                    return Convert.ToInt32(HttpContext.Current.Request.QueryString["oid"]);
                else
                    return -1;
            }
            catch
            {
                return -1;
            }

        }
    }
    private Order _order;
    public Order Order
    {
        get
        {
            if (_order == null)
            {
                _order = OrderManager.GetOrderById(OrderNo);
                SessionManager.OrderNumber = null;
            }
            return _order;
        }
    }

   

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            if (Order == null)
            {
                Response.Redirect(eShop.Business.CustomSiteMap.OrderSummary);
            }

            //this.Header.DataBind();
          
            BindData();
        }
    }

    private void BindData()
    {
        litCompanyaddress.Text = Server.HtmlDecode(string.Format("{0}<br/>{1}<br/>{2}, {3} {4}<br/>Phone: {5}", SiteSettings.CompanyName,SiteSettings.CompanyAddress, SiteSettings.CompanyCity, SiteSettings.CompanyState, SiteSettings.CompanyZip, SiteSettings.CompanyPhone));
        BindProductList();

        
        Address shippingAddress = Order.ShippingAddress;

        lblDate.Text = Order.OrderDate.ToShortDateString();        

        lblShipmentStatus.Text = Order.ShippingStatus.ToString();
        lblPaymentMethod.Text = Order.PaymentType.ToString();
        lblPaymentStatus.Text = Order.OrderStatus.ToString();
        lblOrderNo.Text = Order.OrderNumber + Order.OrderId.ToString();
        lblCustomerNumber.Text = Order.UserId.ToString().PadLeft(10,'0');
        lblSubTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.OrderItems.TotalPrice);
        lblShippingHandling.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.ShippingCost);
        lblTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.TotalAmount);

        lblShippingName.Text = shippingAddress.Name;
        lblShippingAddress.Text = shippingAddress.AddressLine1 + "\n" + shippingAddress.AddressLine2;
        lblShippingCountry.Text = shippingAddress.Country;
        lblShippingPhone.Text = shippingAddress.PhoneNumber;
        lblShippingCityStateZip.Text = string.Format("{0}, {1} {2}", shippingAddress.City, shippingAddress.State, shippingAddress.Zip);
        
        string orderStatus = (Order.OrderStatus.ToString() == OrderStatusType.CCSuccess.ToString()) ? "Success" : "Failed";

        if (Order.OrderStatus == OrderStatusType.CCSuccess && Order.PaymentType == PaymentType.CreditCard)
        {
            uccResponseMessage.SetSuccessMessage(@"Total amount " + ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.TotalAmount) + " have been deducted from your card.");
        }
        else if (Order.OrderStatus == OrderStatusType.CCFail && Order.PaymentType == PaymentType.CreditCard)
        {
            uccResponseMessage.SetFailMessage(@" Failed to deducted amount from your card. Please contact with "+SiteSettings.CompanyName);
        }
        else if (Order.OrderStatus == OrderStatusType.ManualAuthorization && Order.PaymentType == PaymentType.bKash)
        {
            uccResponseMessage.SetSuccessMessage("Order will shipped once your payment is validated through bKash.");
        }
        else
        {
            uccResponseMessage.SetFailMessage("Sorry! Failed to identify the status of your order. Please contact with " + SiteSettings.CompanyName);
        }
        
    }

    private void BindProductList()
    {
        rptProduct.DataSource = Order.OrderItems;
        rptProduct.DataBind();
       
        if (!String.IsNullOrEmpty(Order.SpecialInstruction))
        {
            lblComments.Visible = true;
            lblComments.Text = "<b>Comments</b> :  " + Order.SpecialInstruction;
            lblComments.Style.Add("height", "250px");
        }
    }

    protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            OrderItem cartItem = (OrderItem)e.Item.DataItem;
            if (cartItem != null)
            {
                Image imgItemImage = (Image)e.Item.FindControl("imgItemImage");
                imgItemImage.ImageUrl = ImageResizer.CheckImageMissing(cartItem.ProductDefaultImage.Replace("original", "smallthumb"), eShop.Business.SiteSettings.NoImageSmallThumbnailUrl);              
            }

        }
       
    }
    protected void imbPrintInvoice_Click(object sender, EventArgs e)
    {
        PrintInvoice();
    }

    protected void PrintInvoice()
    {
        string script = "<script type=\"text/javascript\" language=\"javascript\">window.print();</script>";
        ClientScript.RegisterClientScriptBlock(this.GetType(), "printscript", script);
    }
    
}
